﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections;
using ProgNetComponents.Design.TypeConverters;
using System.Drawing.Design;

namespace ProgNetComponents.Data.Query
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class SelectQuery : ICloneable
    {
        private string _Query;
        private string _TableName;
        private string _TableDataSetName;

        [RefreshProperties(System.ComponentModel.RefreshProperties.All)]
        public string TableName
        {
            get
            {
                if (_TableName.IsNull())
                    return _TableDataSetName;
                return _TableName;
            }
            set { _TableName = value; }
        }

        [RefreshProperties(System.ComponentModel.RefreshProperties.All)]
        public string TableDataSetName
        {
            get
            {
                if (_TableDataSetName.IsNull())
                    return _TableName;
                return _TableDataSetName;
            }
            set { _TableDataSetName = value; }
        }

        [RefreshProperties(System.ComponentModel.RefreshProperties.All),
        Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        public string Query
        {
            get
            {
                if (_Query.IsNull())
                {
                    if (TableName.IsNotNull())
                        return "SELECT * FROM " + TableName;
                }
                return _Query;
            }
            set { _Query = value; }
        }

        public override string ToString()
        {
            string str = base.ToString();
            if (TableName.IsNotNull() || TableDataSetName.IsNotNull())
            {
                if (TableName.IsNotNull())
                {
                    str = TableName;
                    if (TableDataSetName != TableName)
                    {
                        str += " [" + TableDataSetName + "]";
                    }
                }
                else
                {
                    str = TableDataSetName;
                }
            }
            return str;
        }

        #region ICloneable Members

        public object Clone()
        {
            SelectQuery q = new SelectQuery();
            q.TableDataSetName = this.TableDataSetName;
            q.TableName = this.TableName;
            q.Query = this.Query;
            return q;
        }
        
        #endregion
    }

    [TypeConverter(typeof(CollectionTypeConverter))]
    [Editor(typeof(ProgNetComponents.Design.Editors.NamedCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
    public class SelectQueryCollection : CollectionBase
    {
        public void Add(SelectQuery item)
        {
            if (item != null)
            {
                List.Add(item);
            }
        }

        public SelectQuery this[int i]
        {
            get
            {
                return List[i] as SelectQuery;
            }
        }

        public void Remove(SelectQuery item)
        {
            if (List.Contains(item))
                List.Remove(item);
        }
    }
}
